from rest_framework import serializers
from .models import (
    CommissionRule, CommissionRecord,
    Invoice, FinancialReport, PaymentTransaction
)


class CommissionRuleSerializer(serializers.ModelSerializer):
    class Meta:
        model = CommissionRule
        fields = [
            'id', 'name', 'description', 'commission_type',
            'commission_rate', 'fixed_amount', 'min_amount', 'max_amount',
            'is_active', 'valid_from', 'valid_to', 'created_at'
        ]
        read_only_fields = ['id', 'created_at']


class CommissionRecordSerializer(serializers.ModelSerializer):
    order_number = serializers.CharField(source='order.order_number', read_only=True)
    
    class Meta:
        model = CommissionRecord
        fields = [
            'id', 'order', 'order_number', 'commission_amount',
            'status', 'created_at', 'paid_at'
        ]
        read_only_fields = ['id', 'created_at']


class CommissionReportSerializer(serializers.Serializer):
    total_commissions = serializers.DecimalField(max_digits=10, decimal_places=2)
    pending_commissions = serializers.DecimalField(max_digits=10, decimal_places=2)
    paid_commissions = serializers.DecimalField(max_digits=10, decimal_places=2)
    commission_rate = serializers.FloatField()


class InvoiceSerializer(serializers.ModelSerializer):
    order_number = serializers.CharField(source='order.order_number', read_only=True)
    
    class Meta:
        model = Invoice
        fields = [
            'id', 'invoice_number', 'order', 'order_number', 'invoice_type',
            'amount', 'tax_amount', 'total_amount', 'status',
            'recipient_name', 'recipient_email', 'recipient_address',
            'issue_date', 'due_date', 'paid_date', 'invoice_file',
            'created_at'
        ]
        read_only_fields = ['id', 'invoice_number', 'created_at']


class InvoiceCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Invoice
        fields = [
            'order', 'invoice_type', 'amount', 'tax_amount',
            'recipient_name', 'recipient_email', 'recipient_address',
            'issue_date', 'due_date'
        ]


class FinancialReportSerializer(serializers.ModelSerializer):
    class Meta:
        model = FinancialReport
        fields = [
            'id', 'report_type', 'start_date', 'end_date', 'description',
            'total_revenue', 'book_sales', 'shipping_income', 'other_income',
            'total_expenses', 'book_costs', 'shipping_costs', 'commissions',
            'operational_costs', 'net_profit', 'report_file', 'created_at'
        ]
        read_only_fields = ['id', 'created_at']


class FinancialReportCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = FinancialReport
        fields = [
            'report_type', 'start_date', 'end_date', 'description'
        ]


class PaymentTransactionSerializer(serializers.ModelSerializer):
    order_number = serializers.CharField(source='order.order_number', read_only=True)
    
    class Meta:
        model = PaymentTransaction
        fields = [
            'id', 'transaction_id', 'order', 'order_number', 'payment_method',
            'amount', 'currency', 'fee_amount', 'net_amount', 'status',
            'gateway', 'gateway_transaction_id', 'gateway_response',
            'created_at', 'completed_at', 'failed_at'
        ]
        read_only_fields = ['id', 'transaction_id', 'created_at']


class PaymentTransactionCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = PaymentTransaction
        fields = [
            'order', 'payment_method', 'amount', 'currency'
        ]


class RevenueReportSerializer(serializers.Serializer):
    date = serializers.DateField()
    revenue = serializers.DecimalField(max_digits=10, decimal_places=2)
    orders = serializers.IntegerField()
    average_order_value = serializers.DecimalField(max_digits=10, decimal_places=2)


class ExpenseReportSerializer(serializers.Serializer):
    date = serializers.DateField()
    book_costs = serializers.DecimalField(max_digits=10, decimal_places=2)
    shipping_costs = serializers.DecimalField(max_digits=10, decimal_places=2)
    commissions = serializers.DecimalField(max_digits=10, decimal_places=2)
    operational_costs = serializers.DecimalField(max_digits=10, decimal_places=2)
    total_expenses = serializers.DecimalField(max_digits=10, decimal_places=2)


class ProfitReportSerializer(serializers.Serializer):
    date = serializers.DateField()
    revenue = serializers.DecimalField(max_digits=10, decimal_places=2)
    expenses = serializers.DecimalField(max_digits=10, decimal_places=2)
    profit = serializers.DecimalField(max_digits=10, decimal_places=2)
    profit_margin = serializers.FloatField()


class PaymentMethodReportSerializer(serializers.Serializer):
    payment_method = serializers.CharField()
    total_amount = serializers.DecimalField(max_digits=10, decimal_places=2)
    transaction_count = serializers.IntegerField()
    average_amount = serializers.DecimalField(max_digits=10, decimal_places=2)


class OutstandingInvoiceSerializer(serializers.Serializer):
    invoice_number = serializers.CharField()
    order_number = serializers.CharField()
    amount = serializers.DecimalField(max_digits=10, decimal_places=2)
    due_date = serializers.DateField()
    days_overdue = serializers.IntegerField()
